/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Family;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.FamilySyncBroker;
import systole.synchronization.remote.entities.FamilyRemote;

/**
 *
 * @author Juan Manuel
 */
public class FamilySyncBrokerDB extends BrokerDB implements FamilySyncBroker {

    public void saveFamilyRemote(FamilyRemote familyRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote family");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(familyRemote);
            this.logger.logDebug("remote family saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save family remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<FamilyRemote> getFamiliesToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Family> getFamiliesToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting family to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select f from FamilyRemote fr right join fr.family f where fr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Family> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get family to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los familiares", e.fillInStackTrace());
        }
    }

    public Family getFamilyByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting family to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    FamilyRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            FamilyRemote familyRemote = (FamilyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (familyRemote != null ? familyRemote.getFamily() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get las remote family synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los familiares", e.fillInStackTrace());
        }
    }

    public FamilyRemote getFamilyRemoteByFamily(Family family) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting family remote by local family");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    FamilyRemote.class).add(Restrictions.eq("family", family)).setMaxResults(1);
            FamilyRemote familyRemote = (FamilyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return familyRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get family by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los familiares", e.fillInStackTrace());
        }
    }

    public int getLastRemoteFamilyIdSynchronized() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting last remote family sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    FamilyRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            FamilyRemote familyRemote = (FamilyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (familyRemote != null ? familyRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote family synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los familiares", e.fillInStackTrace());
        }
    }
}
